class ZhanBu {
  constructor(zyInstance) {
    this.zyInstance = zyInstance // 周易 类实例
  }

  pu() {
    const { gnum, bgnum, byao, bgua } = this.qigua()
    return this.jiegua(gnum, bgnum, byao, bgua)
  }

  bugaindex() {
    let bagua = `kun zhen kan dui gen li xun qian`.split(' ')
    let bagua1 = `di lei shui ze shan huo feng tian`.split(' ')
    let bgindex = `tian_tian tian_ze tian_huo tian_lei tian_feng tian_shui tian_shan tian_di ze_tian ze_ze ze_huo ze_lei ze_feng ze_shui ze_shan ze_di huo_tian huo_ze huo_huo huo_lei huo_feng huo_shui huo_shan huo_di lei_tian lei_ze lei_huo lei_lei lei_feng lei_shui lei_shan lei_di feng_tian feng_ze feng_huo feng_lei feng_feng feng_shui feng_shan feng_di shui_tian shui_ze shui_huo shui_lei shui_feng shui_shui shui_shan shui_di shan_tian shan_ze shan_huo shan_lei shan_feng shan_shui shan_shan shan_di di_tian di_ze di_huo di_lei di_feng di_shui di_shan di_di`.split(
      ' '
    )
    let num = `1 10 13 25 44 6 33 12 43 58 49 17 28 47 31 45 14 38 30 21 50 64 56 35 34 54 55 51 32 40 62 16 9 61 37 42 57 59 53 20 5 60 63 3 48 29 39 8 26 41 21 27 18 4 52 23 11 19 36 24 46 7 15 2`.split(
      ' '
    )
    let yi = {}

    let zy = bgindex.reduce((obj, item, index) => {
      obj[item] = num[index]
      return obj
    }, {})

    Array(64)
      .fill()
      .map((_, i) => i.toString(8))
      .forEach((item, i) => {
        if (/^\d$/.test(item)) {
          yi[item] = bagua1[0] + '_' + bagua1[item]
        } else {
          let [q, k] = item.split('')
          yi[item] = bagua1[q] + '_' + bagua1[k]
        }
      })
    return { zy, yi }
  }

  jiegua(ogua, bgua, myao, mguo) {
    let $int = mguo ? ogua : bgua
    let msg = ''
    // console.log(ogua, bgua, myao, mguo,$int);
    if (myao === 'B') {
      msg = this.zhanbu(ogua, -1)
      msg += '变'
      msg += this.zhanbu(bgua, -1)
    } else if (myao === 'C') {
      if (ogua === 0 || ogua === 63) {
        msg = this.zhanbu(ogua, 6)
      } else {
        msg = this.zhanbu(ogua, -1)
      }
    } else if (myao === 'U') {
      msg = this.zhanbu(ogua, -1)
    } else {
      msg = this.zhanbu($int, myao)
    }
    return msg
  }

  zhanbu(gua, myao) {
    let { zy, yi } = this.bugaindex()
    let sint = gua.toString(8)
    let reply = this.zyInstance.ZhouyiEx(zy[yi[sint]])

    let tuan = this.zyInstance.outtuan(reply)

    let { yao: replyyao, yaoci: syao } = this.zyInstance.maixyao(reply, myao)
    let { yao: replyxiang, yaoci: sxiang } = this.zyInstance.maixiang(
      reply,
      myao
    )

    tuan += sxiang[0]
    let wydsg = ''
    if (myao === -1) {
      wydsg += '卦：' + tuan + '\n\n'
    } else if (myao === 6) {
      wydsg += '爻：' + syao[6] + '\n' + sxiang[7]
    } else {
      wydsg = '卦：' + tuan + '\n\n'
      wydsg += '爻：' + replyyao + '\n' + replyxiang + '\n'
    }
    return wydsg
  }

  qigua() {
    let znum = this.initzhishu()
    let { yinum: gbnum, dec: gnum } = this.sumgua(this.sumyingyan(znum))
    let { dec: bgnum, maiyao: byao, maigua: bgua } = this.biangua(
      this.sumyingyan(znum)
    )
    return { gnum, bgnum, byao, bgua }
  }

  getRandom(minNum, maxNum) {
    return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10)
  }

  initzhishu() {
    let shishu = []
    for (let i = 0; i <= 5; i++) {
      let rand = this.getRandom(6, 9)
      shishu.push(rand)
    }
    // return [8, 9, 7, 8, 8, 8]
    // return shishu
    const date = new Date()
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const min = date.getMinutes()
    const m = date.getSeconds()

    return [year, month, day, 9, 30, 0]
  }

  sumyingyan(shishu) {
    let ying = {},
      yang = {},
      bianyao = {}
    for (let i = 0; i <= 5; i++) {
      let num = shishu[i]
      if (num % 6 === 0 || num % 6 === 2) ying[i] = 1
      if (num % 6 === 1 || num % 6 === 3) yang[i] = 1
      if (num % 6 === 0 || num % 6 === 3) bianyao[i] = 1
    }

    return { ying, yang, bianyao }
  }

  sumgua({ ying, yang, bianyao }) {
    let yinum = '',
      n = 5
    for (let i = 0; i <= 5; i++) {
      n = 5 - i
      if (yang[n]) yinum += '1'
      if (ying[n]) yinum += '0'
    }

    let dec = parseInt(yinum, 2)

    return { yinum, dec }
  }

  biangua({ ying, yang, bianyao }) {
    let { yinum: gnum, dec: num } = this.sumgua({ ying, yang, bianyao })
    let bnum = gnum.split('')
    for (let i = 0; i <= 5; i++) {
      if (bianyao[i]) {
        bnum[i] = bnum[i] === '0' ? 1 : 0
      }
    }

    let dingyao = {}
    for (let i = 0; i <= 5; i++) {
      if (bianyao[i]) {
        continue
      }
      dingyao[i] = 1
    }

    let maiyao = '',
      maigua,
      by = Object.keys(bianyao).sort(),
      dy = Object.keys(dingyao).sort(),
      bunum = Object.keys(bianyao).length

    if (bunum === 1) {
      maiyao = by[0]
      maigua = 1
    } else if (bunum === 2) {
      maiyao = by[1]
      maigua = 1
    } else if (bunum === 3) {
      maiyao = 'B'
      maigua = 1
    } else if (bunum === 4) {
      maiyao = dy[0]
      maigua = 0
    } else if (bunum === 5) {
      maiyao = dy[0]
      maigua = 0
    } else if (bunum === 6) {
      maiyao = 'C'
      maigua = 0
    } else {
      maiyao = 'U'
      maigua = 1
    }
    bnum = bnum.join('')
    let dec = parseInt(bnum, 2)
    console.log(bnum)
    return { dec, maiyao, maigua }
  }
}
